﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kay.Cqp.App.Models;
using Newtonsoft.Json;

namespace Kay.Cqp.App.Common
{
    public static class MySettingsConfig
    {
        public static long Qq { get; set; }

        private static readonly MyConsts C = new MyConsts();

        public static bool ZidongTianjiaHaoyou()
        {
            var path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), nameof(QqConfig));
            if (!File.Exists(path))
                return false;
            var str = File.ReadAllText(path);
            var qqConfig = JsonConvert.DeserializeObject<QqConfig>(str);
            return qqConfig.ManagerIsEnabled && qqConfig.AutoAgreeRequestAddFriend;
        }

        public static bool ZidongFasongHuanyingyu(string qun, out string msg)
        {
            msg = null;
            var path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), nameof(QqConfig));
            if (!File.Exists(path))
            {
                return false;
            }
            var str = File.ReadAllText(path);
            var qqConfig = JsonConvert.DeserializeObject<QqConfig>(str);
            var quns = qqConfig.Groups;
            if (!quns.Contains(qun)) return false;
            path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), qun, nameof(C.SendWelcome));
            if (!File.Exists(path))
                return false;
            msg = File.ReadAllText(path);
            return true;
        }

        public static bool ZidongTichu(string qun, string msg, out bool notAgree)
        {
            notAgree = false;
            var path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), nameof(QqConfig));
            if (!File.Exists(path))
            {
                return false;
            }
            var str = File.ReadAllText(path);
            var qqConfig = JsonConvert.DeserializeObject<QqConfig>(str);
            var quns = qqConfig.Groups;
            if (!quns.Contains(qun)) return false;

            path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), qun, nameof(GroupConfig));
            if (!File.Exists(path))
                return false;
            str = File.ReadAllText(path);
            var groupConfig = JsonConvert.DeserializeObject<GroupConfig>(str);
            notAgree = groupConfig.NotAgreeAddGroupRequest;

            path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), qun, nameof(C.KickKeywords));
            if (!File.Exists(path))
                return false;
            str = File.ReadAllText(path);
            var tirenGuanjianzi = JsonConvert.DeserializeObject<Keyworlds>(str);
            var kws = tirenGuanjianzi.Values.Where(msg.Contains);
            return kws.Any();
        }

        public static bool ZidongJinyan(string qun, string msg, out int day)
        {
            day = 2;
            var path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), nameof(QqConfig));
            if (!File.Exists(path))
            {
                return false;
            }
            var str = File.ReadAllText(path);
            var qqConfig = JsonConvert.DeserializeObject<QqConfig>(str);
            var quns = qqConfig.Groups;
            if (!quns.Contains(qun)) return false;

            path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), qun, nameof(GroupConfig));
            if (!File.Exists(path))
                return false;
            str = File.ReadAllText(path);
            var groupConfig = JsonConvert.DeserializeObject<GroupConfig>(str);
            day = groupConfig.Day;

            path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), qun, nameof(C.GagKeywords));
            if (!File.Exists(path))
                return false;
            str = File.ReadAllText(path);
            var jinyanGuanjianzi = JsonConvert.DeserializeObject<Keyworlds>(str);
            var kws = jinyanGuanjianzi.Values.Where(msg.Contains);
            return kws.Any();
        }

        public static bool ZidongTongyiRuqun(int subType, string qun)
        {
            var path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), nameof(QqConfig));
            if (!File.Exists(path))
            {
                return false;
            }
            var str = File.ReadAllText(path);
            var qqConfig = JsonConvert.DeserializeObject<QqConfig>(str);
            if (subType == 2)
                return qqConfig.AutoAgreeInvitationAddGroup;
            var quns = qqConfig.Groups;
            if (!quns.Contains(qun)) return false;

            path = Path.Combine(C.AppDirectory, C.ConfigDirectory, Qq.ToString(), qun, nameof(GroupConfig));
            if (!File.Exists(path))
                return false;
            str = File.ReadAllText(path);
            var groupConfig = JsonConvert.DeserializeObject<GroupConfig>(str);
            return groupConfig.AutoAgreeAddGroupRequest;
        }
    }
}
